<template>
	<div class="system-dept-container">
		<!-- 查询条件-->
		<el-card shadow="hover">
			<el-form inline v-model="params" size="small">
				<el-form-item>
					<el-input v-model="params.miniappName" placeholder="搜索小程序名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.appId" placeholder="搜索AppId"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" class="ml10" @click="initTableData">
						<el-icon>
							<elementSearch />
						</el-icon>
						查询
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<!--分割线-->
		<el-divider></el-divider>
		<!--数据展示和分页-->
		<el-card shadow="never">
			<template #header>
				<div class="card-header">
					<span>租户小程序配置表列表</span>
					<!--新增等操作区域-->
					<el-button size="small" type="success" class="ml10" @click="openAddWindow">
						<el-icon>
							<elementDocumentAdd />
						</el-icon>
						新增租户小程序配置表
					</el-button>
				</div>
			</template>
			<!--表格展示区-->
			<el-table :data="tableData.data" style="width: 100%">
				<el-table-column align="center" label="小程序ID" prop="miniappId" show-overflow-tooltip />
				<el-table-column align="center" label="小程序名称" prop="miniappName" show-overflow-tooltip />
				<el-table-column align="center" label="appid" prop="appId" show-overflow-tooltip />
				<el-table-column align="center" label="app_secret" prop="appSecret" show-overflow-tooltip />
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="mini" type="text" @click="openEditWindow(scope.row)">修改</el-button>
						<el-popconfirm
							confirm-button-text="删除"
							cancel-button-text="取消"
							icon-color="red"
							title="确定删除这条记录吗？"
							@confirm="delRow(scope.row)"
						>
							<template #reference>
								<el-button size="mini" type="text">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
			<!--分页-->
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="params.pageNo"
				background
				v-model:page-size="params.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>

		<AddTenantMiniapp ref="addRef" @initTableData="initTableData" />
		<EditTenantMiniapp ref="editRef" @initTableData="initTableData" />
	</div>
</template>

<script lang="ts">
import { onMounted, reactive, ref, toRefs } from 'vue';
import { delTenantMiniapp, getTenantMiniappList } from '/@/api/tenant/miniapp';
import AddTenantMiniapp from '/@/views/tenant/miniapp/component/addTenantMiniapp.vue';
import EditTenantMiniapp from '/@/views/tenant/miniapp/component/editTenantMiniapp.vue';
import { useRoute } from 'vue-router';

export default {
	name: 'tenantInfo',
	components: {
		AddTenantMiniapp,
		EditTenantMiniapp,
	},
	setup(props) {
		const addRef = ref();
		const editRef = ref();
		const route = useRoute();

		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
			},
			params: {
				pageNo: 1,
				pageSize: 10,
				miniappName: '',
				appId: '',
				tenantId: '',
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			getTenantMiniappList(state.params).then((res) => {
				state.tableData.data = res.data;
				state.tableData.total = res.extra.total;
			});
		};

		//分页大小改变
		const onHandleSizeChange = (val: number) => {
			state.params.pageSize = val;
			initTableData();
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.params.pageNo = val;
			initTableData();
		};

		// 打开新增菜单弹窗
		const openAddWindow = () => {
			addRef.value.openWindows(state.params.tenantId);
		};

		const openEditWindow = (row: any) => {
			editRef.value.openWindows(row.miniappId, state.params.tenantId);
		};

		const delRow = (row: any) => {
			delTenantMiniapp(row.miniappId).then(() => {
				initTableData();
			});
		};

		// 页面加载时
		onMounted(() => {
			state.params.tenantId = route.query.tenantId;
			initTableData();
		});

		return {
			addRef,
			editRef,
			initTableData,
			onHandleSizeChange,
			onHandleCurrentChange,
			openAddWindow,
			openEditWindow,
			delRow,
			...toRefs(state),
		};
	},
};
</script>

<style scoped></style>
